﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Itfort;
using MySoft.Data;

namespace WanPiJie
{
	/// <summary>
	/// 
	/// </summary>
	public class GrowthRecordService : IGrowthRecordService
	{
		private DbSession dbSession = new DbSession("Ehr");

		/// <summary>
		/// 添加成长记录。
		/// </summary>
		/// <param name="babyId">宝宝编号。</param>
		/// <param name="surveyDate">测量日期。</param>
		/// <param name="weight">身高。</param>
		/// <param name="height">体重。</param>
		/// <param name="headSize">头围。</param>
		/// <param name="chestSize">胸围。</param>
		public GrowthRecord AddGrowthRecord(int babyId, DateTime surveyDate, double height, double weight, double headSize, double chestSize)
		{
			_GrowthRecord _record = new _GrowthRecord();
			_record.AddTime = DateTime.Now;
			_record.UpdateTime = _record.AddTime;
			_record.BabyId = babyId;
			_record.SurveyDate = surveyDate;
			_record.Height = height;
			_record.Weight = weight;
			_record.HeadSize = headSize;
			_record.ChestSize = chestSize;

			dbSession.Save(_record);
			return GetGrowthRecord(_record.Id);
		}

		/// <summary>
		/// 获取成长记录。
		/// </summary>
		/// <param name="id">记录编号。</param>
		/// <returns></returns>
		public GrowthRecord GetGrowthRecord(int id)
		{
			return dbSession.Single<_GrowthRecord>(_GrowthRecord._.Id == id)
				.Convert2<GrowthRecord>();
		}

		/// <summary>
		/// 获取成长记录。
		/// </summary>
		/// <param name="babyId">宝宝编号。</param>
		/// <returns></returns>
		public List<GrowthRecord> GetGrowthRecords(int babyId)
		{
			return dbSession.From<_GrowthRecord>()
				.Where(_GrowthRecord._.BabyId == babyId)
				.ToList()
				.OrderBy(p => p.SurveyDate)
				.Convert2<GrowthRecord>();
		}

		/// <summary>
		/// 获取成长记录。
		/// </summary>
		/// <param name="babyId">宝宝编号。</param>
		/// <returns></returns>
		public Int32 GetGrowthRecordCount(int babyId)
		{
			return dbSession.From<_GrowthRecord>()
				.Where(_GrowthRecord._.BabyId == babyId).Count();
		}

		/// <summary>
		/// 获取第一条成长记录。
		/// </summary>
		/// <param name="babyId">宝宝编号。</param>
		/// <returns></returns>
		public GrowthRecord GetFirstGrowthRecord(int babyId)
		{
			return dbSession.From<_GrowthRecord>()
				.Where(_GrowthRecord._.BabyId == babyId)
				.OrderBy(_GrowthRecord._.SurveyDate.Asc)
				.GetTop(1)
				.ToSingle()
				.Convert2<GrowthRecord>();
		}

		/// <summary>
		/// 获取最后一条成长记录。
		/// </summary>
		/// <param name="babyId">宝宝编号。</param>
		/// <returns></returns>
		public GrowthRecord GetLastGrowthRecord(int babyId)
		{
			return dbSession.From<_GrowthRecord>()
				.Where(_GrowthRecord._.BabyId == babyId)
				.OrderBy(_GrowthRecord._.SurveyDate.Desc)
				.GetTop(1)
				.ToSingle()
				.Convert2<GrowthRecord>();
		}

		/// <summary>
		/// 获取宝宝出生时的规格。
		/// </summary>
		/// <param name="baby">宝宝对象。</param>
		/// <returns></returns>
		public GrowthRecord GetBirthGrowthRecord(Baby baby)
		{
			WhereClip where = _GrowthRecord._.BabyId == baby.Id && _GrowthRecord._.SurveyDate == baby.Birthday;
			return dbSession.Single<_GrowthRecord>(where).Convert2<GrowthRecord>();
		}

		/// <summary>
		/// 是否存在宝宝成长记录。
		/// </summary>
		/// <param name="babyId">宝宝编号。</param>
		/// <returns></returns>
		public bool ExistsGrowthRecords(int babyId)
		{
			return dbSession.Exists<_GrowthRecord>(_GrowthRecord._.BabyId == babyId);
		}

		/// <summary>
		/// 修改成长记录。
		/// 宝宝日期和测量日期作为条件。
		/// </summary>
		/// <param name="babyId">宝宝编号。</param>
		/// <param name="surveyDate">测量日期。</param>
		/// <param name="weight">身高。</param>
		/// <param name="height">体重。</param>
		/// <param name="headSize">头围。</param>
		/// <param name="chestSize">胸围。</param>
		public void UpdateGrowthRecord(int babyId, DateTime surveyDate, double height, double weight, double headSize, double chestSize)
		{
			List<FieldValue> fieldValues = new List<FieldValue>();
			fieldValues.Add(new FieldValue(_GrowthRecord._.Height, height));
			fieldValues.Add(new FieldValue(_GrowthRecord._.Weight, weight));
			fieldValues.Add(new FieldValue(_GrowthRecord._.HeadSize, headSize));
			fieldValues.Add(new FieldValue(_GrowthRecord._.ChestSize, chestSize));

			WhereClip where = _GrowthRecord._.BabyId == babyId && _GrowthRecord._.SurveyDate == surveyDate;

			dbSession.Update<_GrowthRecord>(fieldValues.ToArray(), where);
		}
	}
}
